package edu.zzuli.model.core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.acegisecurity.Authentication;
import org.acegisecurity.context.SecurityContextHolder;
import org.apache.struts2.ServletActionContext;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import edu.zzuli.common.Constants_core;
import edu.zzuli.common.MyVisit;
import edu.zzuli.model.core.iface.LoginService;

/**
 * @author tianshaojie
 * @date 2011-1-6
 * @discription : 登录验证通过后调用的Action，主要用来设置登录用户信息。
 */
@Controller
public class AuthenticatedAction {
	
	@Resource
	private LoginService loginService;
	
	public String setMyVisitInfo() {
		ActionContext.getContext().getSession().put("ctx", ServletActionContext. getRequest().getContextPath());
		Authentication au = null;
		if (SecurityContextHolder.getContext() != null)
			au = SecurityContextHolder.getContext().getAuthentication();
		if (au == null || au.getName() == null || au.getName().length() == 0) {
			SecurityContextHolder.clearContext();
			return "error";
		} else {
			MyVisit myVisit = new MyVisit();
			myVisit.setLoginInfoMap(this.loginService.saveLoginInfo(au.getName(), ServletActionContext. getRequest().getRemoteAddr()));
			ActionContext.getContext().getSession().put("myVisit",myVisit);
			ActionContext.getContext().getSession().put(Constants_core.LOGIN_IP, ServletActionContext. getRequest().getRemoteAddr());
//			ServletActionContext. getRequest().getSession().setAttribute("myVisit",myVisit);
//			ServletActionContext. getRequest().getSession().setAttribute(Constants_core.LOGIN_IP, ServletActionContext. getRequest().getRemoteAddr());
			List<Map> listXTGN = getListXTGN(myVisit);
			if (listXTGN != null && listXTGN.size() > 0) {
				List<Map> xtgnList = new ArrayList<Map>();
				for(Map map : listXTGN) {
					if(Integer.valueOf(map.get("jdlb").toString()) == 1) {
						String xtgnId = map.get("id").toString();
						Map titleMap = new HashMap();
						titleMap.put("title", map.get("jdmc"));
						List<Map> contentList = new ArrayList<Map>();
						for(Map gnMap : listXTGN) {
							if(Integer.valueOf(gnMap.get("jdlb").toString()) == 2 && gnMap.get("fid").toString().equals(xtgnId)) {
								Map mapContent = new HashMap<String, String>();
								mapContent.put("gndz", gnMap.get("gndz"));
								mapContent.put("jdmc", gnMap.get("jdmc"));
								contentList.add(mapContent);
							}
						}
						titleMap.put("content", contentList);
						xtgnList.add(titleMap);
					}
				}
				if (xtgnList.size() > 0) {
					ActionContext.getContext().getSession().put("xtgnList",xtgnList);
				}
			}
			return "success";
		}
	}
	
	public List<Map> getListXTGN(MyVisit myVisit) {
		String sql = "";
//		if (this.myVisit.getGG_JGBH().getJglb() == Constants_core.JGSF_ZBJG) {
//			sql = "select gn.*, SYS_CONNECT_BY_PATH(to_char(gn.sxh, '000'), '.') sort\n"
//					+ "  from (select distinct t.*\n"
//					+ "          from gg_xtgn t\n "
//					+ "         start with t.id in (select gg_lbgn.gnid\n"
//					+ "                               from gg_lbgn, gg_lbfp, gg_czyb_gwlb\n"
//					+ "                              where gg_lbgn.lbid = gg_lbfp.lbid\n"
//					+ "                                and gg_lbfp.fpid = gg_czyb_gwlb.gwid\n"
//					+ "                                and gg_czyb_gwlb.czyid = :GG_CZYB_ID "
//					+ " union select gg_lbgn.gnid\n"
//					+ "                               from gg_lbgn, gg_lbfp\n"
//					+ "                              where gg_lbgn.lbid = gg_lbfp.lbid\n"
//					+ "                                and gg_lbfp.fpid =:GG_CZYB_ID"
//					+ " union select gg_czyb_lbgn.xtgn from gg_czyb_lbgn where gg_czyb_lbgn.czyxx=:GG_CZYB_ID)\n"
//					+ "        connect by prior t.fid = t.id) gn\n"
//					+ " start with gn.fid is null\n"
//					+ "connect by gn.fid = prior gn.id and  gn.id is not null\n"
//					+ " order by sort";
//
//		} else {
			sql = "select gn.*, SYS_CONNECT_BY_PATH(to_char(gn.sxh, '000'), '.') sort\n"
					+ "  from (select distinct t.*\n"
					+ "          from gg_xtgn t\n "
					+ "         start with t.id in (select gg_lbgn.gnid\n"
					+ "                               from gg_lbgn, gg_lbfp\n"
					+ "                              where gg_lbgn.lbid = gg_lbfp.lbid\n"
					+ "                                and gg_lbfp.fpid =:GG_CZYB_ID "
					+		" union select GG_CZYB_LBGN.xtgn from GG_CZYB_LBGN where GG_CZYB_LBGN.czyxx=:GG_CZYB_ID\n"
					+		" union select GG_LBGN.gnid from GG_CZYB_GWLB,GG_LBFP,GG_LBGN where GG_CZYB_GWLB.Gwid = GG_LBFP.Fpid and GG_LBFP.Lbid = GG_LBGN.Lbid and GG_CZYB_GWLB.CZYID =:GG_CZYB_ID)\n"
					+ "        connect by prior t.fid = t.id) gn\n"
					+ " start with gn.fid is null\n"
					+ "connect by gn.fid = prior gn.id and  gn.id is not null\n"
					+ " order by sort";
//		}
		List<Map> listXTGN = this.loginService.queryForList(sql, new MapSqlParameterSource("GG_CZYB_ID", myVisit.getCzy().getId()));
//		List<Map> listXTGNTemp = new ArrayList<Map>();
//		if (isGys() && (!isZSGLGys())) {
//			for (Map map : this.listXTGN) {
//				if (map.get("ID").equals("XT_ZSGL_ZSSFQR")) {
//				} else {
//					listXTGNTemp.add(map);
//				}
//			}
//			this.listXTGN = listXTGNTemp;
//		}
		return listXTGN;
	}
}
